package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.HydrologicDevicePoint;
import com.cray.intelligenthydrologyback.client.domain.entity.SysAlgorithmMode;
import com.cray.intelligenthydrologyback.client.model.AbstractResponseResult;
import com.cray.intelligenthydrologyback.client.model.ResponseListResult;
import com.cray.intelligenthydrologyback.client.model.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/device")
@Api(value = "Device Point Controller", tags = "设备点位")
public class DevicePointController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/point/{id}")
    @ApiOperation(httpMethod = "GET", value = "详情")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysAlgorithmMode> getPointInfo(
            @ApiParam(value = "id", required = true)
            @PathVariable(value = "id") Long id) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/device/point/{id}")
                        .build(id)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @PostMapping("/device/point")
    @ApiOperation(httpMethod = "POST", value = "增加")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<HydrologicDevicePoint> addHydrologicDevicePoint(
            @RequestBody HydrologicDevicePoint hydrologicDevicePoint) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()  // 使用 POST 请求
                .uri("/device/device/point")  // 请求的 URL
                .bodyValue(hydrologicDevicePoint) // 请求体，可以是任何对象
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @PostMapping("/point/list")
    @ApiOperation(httpMethod = "GET", value = "获取列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseListResult<HydrologicDevicePoint> getPointList(
            @ApiParam(value = "hydrologicDevicePoint", required = true)
            @RequestBody HydrologicDevicePoint hydrologicDevicePoint) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/device/device/list")
                        .build(hydrologicDevicePoint)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseListResult.class);
    }

    @DeleteMapping("/point/{ids}")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult deletePointList(
            @ApiParam(value = "ids", required = true)
            @PathVariable(value = "ids") Long ids) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().delete()
                .uri(uriBuilder -> uriBuilder
                        .path("/device/point/{ids}")
                        .build(ids)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @PutMapping("/point")
    @ApiOperation(httpMethod = "PUT", value = "修改")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult updateHydrologicDevicePoint(
            @ApiParam(value = "hydrologicDevicePoint", required = true)
            @RequestBody HydrologicDevicePoint hydrologicDevicePoint) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().delete()
                .uri(uriBuilder -> uriBuilder
                        .path("/device/point")
                        .build(hydrologicDevicePoint)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @GetMapping("/point/current-value/{id}")
    @ApiOperation(httpMethod = "GET", value = "详情")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysAlgorithmMode> getCurrentValue(
            @ApiParam(value = "id", required = true)
            @PathVariable(value = "id") Long id) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/device/point/current-value/{id}")
                        .build(id)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }
}
